package com.shoeshopping.service.zx;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.shoeshopping.dao.BaseDao;
import com.shoeshopping.entity.Goods;
import com.shoeshopping.formbean.zx.GoodsAdminForm;
import com.shoeshopping.utils.zx.EasyUIResult;

@Service("goodsAdminService")
@Transactional
public class GoodsAdminServiceImpl implements GoodsAdminService {

	@Resource
	BaseDao baseDao;
	
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public EasyUIResult list(GoodsAdminForm adminForm) {
		
		String hql = "from Goods as g";
		List allList = baseDao.find(hql);
		List list = baseDao.findWithPageAndSort(hql, null, 
				adminForm.getPage(), adminForm.getRows(),
				adminForm.getSort(), adminForm.getOrder());
		
		EasyUIResult result = new EasyUIResult((long) allList.size(), list);
		return result;
	}

	/*将源文件复制成目标文件*/
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	private void copy(File source,File target){
		final int BUFF_SIZE = 40*40;
		InputStream inputStream = null;
		OutputStream outputStream = null;
		try{
			inputStream = new BufferedInputStream(new FileInputStream(source),BUFF_SIZE);
			outputStream = new BufferedOutputStream(new FileOutputStream(target),BUFF_SIZE);
			
			byte[] buffer = new byte[BUFF_SIZE];
			int len = 0;
			while((len = inputStream.read(buffer)) > 0){
				outputStream.write(buffer,0,len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(inputStream != null){
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(outputStream != null){
				try {
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	
	@Override
	public boolean add(GoodsAdminForm adminForm) {
		
		Goods goods = new Goods();
		if(adminForm.getId() != null && adminForm.getId().length() > 0){
			System.out.println("ID = "+adminForm.getId());
			goods.setId(adminForm.getId().trim());
		}
		goods.setName(adminForm.getName().trim());
		goods.setColor(adminForm.getColor().trim());
		goods.setRest(adminForm.getRest());
		goods.setPrice(adminForm.getPrice());
		goods.setType(adminForm.getType().trim());
		
		if(adminForm.getDetails() != null){
			goods.setDetails(adminForm.getDetails().trim());
		}
		
		if(adminForm.getUploadFileName() != null){
			System.out.println("adminForm.getUploadFileName() = " +adminForm.getUploadFileName());
			/*保存文件*/
			String realPath = ServletActionContext.getServletContext().getRealPath(adminForm.getSavePath())+"\\" +
			adminForm.getUploadFileName();
			System.out.println("realPath = "+realPath);
			String imagePath = adminForm.getSavePath()+adminForm.getUploadFileName();
			System.out.println("imagePath = "+imagePath);
			goods.setImgPath(imagePath);
			
			File target = new File(realPath);
			copy(adminForm.getUpload(),target);
		} else{
			goods.setImgPath(adminForm.getImgPath());
		}
		
		
		
		
		baseDao.save(goods);
		return true;
	}

	@Override
	public void delete(GoodsAdminForm dataform) {
		Goods goods = new Goods();
		goods.setId(dataform.getId());
		baseDao.delete(goods);
	}

	@Override
	public Goods edit(GoodsAdminForm dataform) {
		String hql = "from Goods as g where g.id=?";
		List parma = new ArrayList();
		parma.add(dataform.getId());
		Goods goods = (Goods) baseDao.find(hql, parma).get(0);
		
		return goods;
		
	}
}
